package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.EquipRepairSpareInfo;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.EquipRepairSpareInfoDao;
import com.efast.dmom.mes.entity.EquipRepairSpareInfoEntity;
import com.efast.dmom.mes.service.EquipRepairSpareInfoService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Service
public class EquipRepairSpareInfoServiceImpl implements EquipRepairSpareInfoService {

	@Autowired
	private EquipRepairSpareInfoDao equipRepairSpareInfoDao;

	@Override
	public ResultBean save(EquipRepairSpareInfo equipRepairSpareInfo) {
		ResultBean resultBean = new ResultBean(true, ' ');
		List<EquipRepairSpareInfoEntity> spareInfoList = equipRepairSpareInfo.getSpareInfoList();
		if (spareInfoList != null && spareInfoList.size() > 0) {
			List<EquipRepairSpareInfoEntity> repairSpareInfoList = equipRepairSpareInfoDao
					.getByRepairInfoId(spareInfoList.get(0).getRepairInfoId());
			if (repairSpareInfoList != null && repairSpareInfoList.size() > 0) {
				boolean flag = true;
				String message = " ";
				for (EquipRepairSpareInfoEntity data : spareInfoList) {
					for (EquipRepairSpareInfoEntity entity : repairSpareInfoList) {
						if (data.getSpareId() == entity.getSpareId()) {
							flag = false;
							if (message.equals(" ")) {
								message = "该维修设备下已存在备件编码为[" + data.getSpareCode();
							} else {
								message = message.concat("、" + data.getSpareCode());
							}
						}
					}
					data.setCompanyCode(SpringWebUtils.getRequestCompany());
					data.setCreationTime(TimeUtil.getLocalDateTime());
					data.setCreator(SpringWebUtils.getRequestUser());
					data.setRowStatus(0);
				}
				if (!flag) {
					resultBean.setSuccess(false);
					resultBean.setMessage(message.concat("]的备件！"));
					return resultBean;
				}
			} else {
                for (EquipRepairSpareInfoEntity data : spareInfoList) {
					data.setCompanyCode(SpringWebUtils.getRequestCompany());
					data.setCreationTime(TimeUtil.getLocalDateTime());
					data.setCreator(SpringWebUtils.getRequestUser());
					data.setRowStatus(0);	
                }
			}
			equipRepairSpareInfoDao.saveBatch(spareInfoList);
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功！");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(EquipRepairSpareInfo equipRepairSpareInfo) {
		ResultBean resultBean = new ResultBean(true, ' ');
		List<EquipRepairSpareInfoEntity> spareInfoList = equipRepairSpareInfo.getSpareInfoList();
		if (spareInfoList != null && spareInfoList.size() > 0) {
			for (EquipRepairSpareInfoEntity data:spareInfoList) {
				data.setLastModifiedTime(TimeUtil.getLocalDateTime());
				data.setLastModifier(SpringWebUtils.getRequestUser());
				equipRepairSpareInfoDao.update(data);
				resultBean.setSuccess(true);
				resultBean.setMessage("更新成功！");
			}
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(EquipRepairSpareInfo equipRepairSpareInfo) {
		ResultBean resultBean = new ResultBean(true, ' ');
		List<EquipRepairSpareInfoEntity> spareInfoList = equipRepairSpareInfo.getSpareInfoList();
		if (spareInfoList != null && spareInfoList.size() > 0) {
			for (EquipRepairSpareInfoEntity data:spareInfoList) {
				data.setLastModifiedTime(TimeUtil.getLocalDateTime());
				data.setLastModifier(SpringWebUtils.getRequestUser());
			}
			equipRepairSpareInfoDao.deleteBatch(spareInfoList);
			resultBean.setSuccess(true);
			resultBean.setMessage("删除成功！");
		}
		return resultBean;
	}

	@Override
	public ResultBean getByRepairInfoId(long repairInfoId) {
		ResultBean resultBean = new ResultBean(true, ' ');
		List<EquipRepairSpareInfoEntity> spareList = equipRepairSpareInfoDao.getByRepairInfoId(repairInfoId);
		if (spareList != null && spareList.size() > 0) {
			resultBean.setSuccess(false);
		} else {
			resultBean.setSuccess(true);
		}
		return resultBean;
	}

	@Override
	public void deleteByRepairInfoId(long repairInfoId) {
		EquipRepairSpareInfoEntity equipRepairSpareInfoEntity = new EquipRepairSpareInfoEntity();
		equipRepairSpareInfoEntity.setRepairInfoId(repairInfoId);
		equipRepairSpareInfoEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		equipRepairSpareInfoEntity.setLastModifier(SpringWebUtils.getRequestUser());
		equipRepairSpareInfoDao.deleteByRepairInfoId(equipRepairSpareInfoEntity);
	}

}
